#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年3月5日

@author: chenyongbing
'''
import sys,os,re,datetime
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
import commands
sys.path.append(os.path.join(current_dir,'../'))
from base.PyMySQL import ConnectMySQL

from base.localConfig import baseLocalConfig

import sys,datetime
rawdb_host = baseLocalConfig.mysqlConfig['rawdb']['host']
rawdb_user = baseLocalConfig.mysqlConfig['rawdb']['user']
rawdb_passwd = baseLocalConfig.mysqlConfig['rawdb']['passwd']
rawdb_db = baseLocalConfig.mysqlConfig['rawdb']['db']

myRawdb = ConnectMySQL(rawdb_host,rawdb_user,rawdb_passwd,rawdb_db)

class StatDeviceCollCount():
    def __init__(self):
        pass
    
    
    def stat_device_coll_count(self,tab='device_coll_count',tmppath='',rawfile=''):
        commands.getstatusoutput('rm -rf %s/*'%tmppath)
        try:
            os.mkdir(tmppath)
        except:pass
        os.chdir(tmppath)
        try:
            day = re.search('([\d]{4}-[\d]{2}-[\d]{2})',rawfile).group(1)
        except:
            day = datetime.date.today().strftime('%Y-%m-%d')
        cmd = "awk -F '.' '{if ($2!=null) print $0 >> $2 }' %s"%(rawfile)
        #print cmd
        #return
        status,out = commands.getstatusoutput(cmd)
        datas = []
        #sql = 'insert into %s'%tab+' (day,device,count) VALUES(%s,%s,%s)'
        sql = 'insert into %s'%tab+' set day=%s,device=%s,count=%s  ON DUPLICATE KEY UPDATE count=%s'
        grfiles = os.listdir(tmppath)
        for grfile in grfiles:
            #print grfile
            status,out = commands.getstatusoutput('cat %s | wc -l'%grfile)
            print day,grfile,int(out.strip())
            times = int(out.strip())
            datas.append([day,grfile,times,times])
        myRawdb.executemany(sql, datas)
            
            
if __name__=='__main__': 
    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--day',metavar=u"day",default=None)

    args = parser.parse_args()           
    day = args.day
    if day==None:
        day = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
    
    myStatDeviceCollCount = StatDeviceCollCount()
    
    rawfile = os.path.join(current_dir,'../log/rawdata_fro_django.%s.txt'%day)
    tmppath = os.path.join(current_dir,'../tmp/rawdatatmp')
    myStatDeviceCollCount.stat_device_coll_count(tmppath=tmppath,rawfile=rawfile)